package com.springmvc.demo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springmvc.demo.mapper.ChronicDiseaseCardMapper;
import com.springmvc.demo.mapper.ChronicDiseasePolicyMapper;
import com.springmvc.demo.mapper.RefundMapper;
import com.springmvc.demo.mapper.RegNCMSMapper;
import com.springmvc.demo.model.ChronicDiseaseCard;
import com.springmvc.demo.model.ChronicDiseasePolicy;
import com.springmvc.demo.model.RefundRegistryForm;
import com.springmvc.demo.model.RegistryFormOfNcms;
import com.springmvc.demo.service.ChronicService;

@Service
public class ChronicServiceImpl implements ChronicService {
	@Autowired
	ChronicDiseaseCardMapper chronicDiseaseCardMapper;

	@Autowired
	RegNCMSMapper regNCMSMapper;

	@Autowired
	RefundMapper refundMapper;

	@Autowired
	ChronicDiseasePolicyMapper chronicDiseasePolicyMapper;

	@Override
	public String addChronicCard(ChronicDiseaseCard cdc) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("person_id", cdc.getPersonId());
		List<?> list = chronicDiseaseCardMapper.selectByMap(map);
		if (list.size() > 0) {
			return "已存在此人的慢性病证信息！";
		} else {
			int i = chronicDiseaseCardMapper.insert(cdc);

			if (i > 0)
				return "添加成功";
			else
				return "添加失败";
		}

	}

	@Override
	public List<ChronicDiseaseCard> getChronicCardList() {
		// TODO Auto-generated method stub

		return chronicDiseaseCardMapper.selectList(null);
	}

	@Override
	public String refundCost(RefundRegistryForm rf) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id_card", rf.getIdCard());
		
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("chronic_disease_card_no", rf.getChronicDiseaseCardNo());
		map3.put("person_id", rf.getIdCard());
		List<ChronicDiseaseCard> ls3=chronicDiseaseCardMapper.selectByMap(map3);//查询此身份证是否登记慢性病信息
		
		List<RegistryFormOfNcms> ls = regNCMSMapper.selectByMap(map);//查询此身份证是否参合

		if (ls.size() > 0&&ls3.size()>0) {//已参合且已登记慢病信息 继续

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			Date date = new Date();
			String year = sdf.format(date);
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("year", year);
			float refund = 0;

			// 获取当前报销政策
			ChronicDiseasePolicy currentPolicy = chronicDiseasePolicyMapper.selectByMap(map1).get(0);

			String idCard = rf.getIdCard();
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("id_card", idCard);
			
			int i = 0;
			float rest = 0;
			List<?>  ls2= refundMapper.selectByMap(map2);//查询是否已存在报销信息
			if (ls2.size()>0) {
				RefundRegistryForm r=(RefundRegistryForm)ls2.get(0);
				
				refund = rf.getTreatmentCost() * currentPolicy.getRatio();
				rest = r.getRestTopLine();
				
				if((rest - refund)<=0) { //超过封顶线的情况
					refund=rest;
					rest=0;					
				}else{
					rest-=refund;
				}
					
				rf.setId(r.getId());
				rf.setRefund(refund);
				rf.setRestTopLine(rest);
				i = refundMapper.updateById(rf);
				
				
				map.clear();
				map1.clear();
				map2.clear();
				map=map1=map2=null;
				return "报销成功，数据已更新，报销金额为："+refund+" 余额为："+rest;
			} else {

				rf.setRestTopLine(currentPolicy.getTopLine());
				refund = rf.getTreatmentCost() * currentPolicy.getRatio();
				rest = rf.getRestTopLine();
				
				if((rest - refund)<=0) { //超过封顶线的情况
					refund=rest;
					rest=0;					
				}else{
					rest-=refund;
				}
				rf.setRefund(refund);
				rf.setRestTopLine(rest);
				i = refundMapper.insert(rf);
				map.clear();
				map1.clear();
				map2.clear();
				map=map1=map2=null;
				if (i > 0)
					return "报销成功，报销金额为："+refund+" 余额为："+rest;
				else
					return "报销失败";
			}
		} else {
			map.clear();

			return "此人未参合或未登记慢病信息";
		}
	}

	@Override
	public List<RefundRegistryForm> getRefundList() {
		// TODO Auto-generated method stub

		return refundMapper.selectList(null);
	}

}
